<template>
  <view class="container">
    <uni-segmented-control
      :current="currentIndex"
      :values="items"
      style-type="text"
      active-color="#007aff"
      @clickItem="onClickItem"
    />

    <view v-if="currentIndex === 0">
      <view style="padding: 10px;"></view>
      <view v-for="(item, idx) in listWeiwancheng" :key="idx">
        <view class="card" v-if="item.sta == 0" @click="toAuditWeiwancheng(idx)">
          <view class="card-title">
            <uni-row>
              <uni-col :span="24">
                <view style="font-weight: bold;">
                  <uni-tag text="审核" size="mini" type="warning"></uni-tag>
                  {{getTypeStr(item.typ)}}
                </view>
              </uni-col>
            </uni-row>
          </view>
          <view class="card-body">
            <text>
							{{(item.content || "").replace(/<br>/g, '\n')}}<br>
						</text>
            <br />
          </view>
          <view class="card-footer">
            <uni-row>
              <uni-col :span="24">
                <view
                  style="text-align: right;color: #999999;"
                >{{getFormatedTimeStr(item.createTime)}}</view>
              </uni-col>
            </uni-row>
          </view>
        </view>
        <view style="padding-bottom: 10px;" v-if="item.sta == 0"></view>
      </view>
      <uni-load-more :status="moreStatusWeiwancheng"></uni-load-more>
    </view>

    <view v-if="currentIndex === 1">
      <view style="padding: 10px;"></view>
      <view v-for="(item, idx) in listYiwancheng" :key="idx">
        <view class="card" v-if="item.sta > 0" @click="toAuditYiwancheng(idx)">
          <view class="card-title">
            <uni-row>
              <uni-col :span="24">
                <view style="font-weight: bold;">
                  <uni-tag text="审核" size="mini" type="warning" v-if="item.sta == 0"></uni-tag>
                  <uni-tag text="通过" size="mini" type="primary" v-if="item.sta == 1"></uni-tag>
                  <uni-tag text="拒绝" size="mini" type="error" v-if="item.sta == 2"></uni-tag>
                  <uni-tag text="撤销" size="mini" type="default" v-if="item.sta == 3"></uni-tag>
                  {{getTypeStr(item.typ)}}
                </view>
              </uni-col>
            </uni-row>
          </view>
          <view class="card-body">
            <text>
							{{item.content.replace(/<br>/g, '\n')}}<br>
						</text>
          </view>
          <view class="card-footer">
            <view style="text-align: right;color: #999999;">{{getFormatedTimeStr(item.createTime)}}</view>
          </view>
        </view>
        <view style="padding-bottom: 10px;" v-if="item.sta >0"></view>
      </view>

      <uni-load-more :status="moreStatusYiwancheng"></uni-load-more>
    </view>
  </view>
</template>

<script>
import { websock } from "../../net/websock";
import moment from "moment";

export default {
  data() {
    return {
      listYiwancheng: [],
      listWeiwancheng: [],
      moreStatusYiwancheng: "more",
      moreStatusWeiwancheng: "more",
      currentIndex: 0,
      items: ["待完成", "已完成"]
    };
  },
  onLoad() {
    uni.$on("TASK-NEW", this.taskNew);
    uni.$on("TASK-REVERT", this.taskNew);
    // uni.$on('TASK-CHANGE', this.taskNew)
    uni.$on("agree_task", this.taskNew);
    uni.$on("reject_task", this.taskNew);
    uni.$on("confirm_task", this.taskNew);
  },
  onUnload() {
    uni.$off("TASK-NEW", this.taskNew);
    uni.$off("TASK-REVERT", this.taskNew);
    // uni.$off('TASK-CHANGE', this.taskNew)
    uni.$off("agree_task", this.taskNew);
    uni.$off("reject_task", this.taskNew);
    uni.$off("confirm_task", this.taskNew);
  },
  onReady() {
    this.getNewestTasksWeiwancheng();
    this.getNewestTasksYiwancheng();
  },
  // 下拉刷新，获取最新的数据
  onPullDownRefresh() {
    console.log("onPullDownRefresh");
    if (this.currentIndex == 0) {
      this.getNewestTasksWeiwancheng();
    } else if (this.currentIndex == 1) {
      this.getNewestTasksYiwancheng();
    }
  },
  // 上拉刷新，获取历史数据
  onReachBottom() {
    console.log("onReachBottom");
    if (this.currentIndex == 0) {
      this.getOlderTasksWeiwancheng();
    } else if (this.currentIndex == 1) {
      this.getOlderTasksYiwancheng();
    }
  },
  methods: {
    getTypeStr(typ) {
				if (typ == 0) {
					return "请假";
				}
				if (typ == 1) {
					return "出差";
				}
				if (typ == 2) {
					return "加班";
				}
				if (typ == 3) {
					return "入职";
				}
				if (typ == 4) {
					return "转正";
				}
				if (typ == 5) {
					return "离职";
				}
				if (typ == 6) {
					return "督办单";
				}
				if (typ == 7) {
					return "会议室";
				}
				if (typ == 8) {
					return "接待单";
				}
				if (typ == 9) {
					return "车辆加油";
				}
				if (typ == 10) {
					return "用车申请";
				}
				if (typ == 11) {
					return "用印申请";
				}
				if (typ == 13) {
					return "采购单";
				}
				if (typ == 14) {
					return "物资领用";
				}
				if (typ == 15) {
					return "合同审核";
				}
				if (typ == 16) {
					return "招标文件";
				}
				if (typ == 17) {
					return "传阅文件";
				}
				if (typ == 18) {
					return "公文流转";
				}
				if (typ == 19) {
					return "补卡审核";
				}
				if (typ == 20) {
					return "会议通知";
				}
				if (typ == 21) {
					return "工作通知";
				}
				if (typ == 22) {
					return "排班详情";
				}
				if (typ == 23) {
					return "内部发文";
				}
				if (typ == 24) {
					return "新建事项";
				}
				if (typ == 25) {
					return "宣传报道";
				}
				if (typ == 26) {
					return "工作餐";
				}
				return "其它";
			},
    toAuditYiwancheng(idx) {
      console.log("auditor yiwancheng", idx, this.listYiwancheng);
      if (this.listYiwancheng[idx].typ == 0) {
        // 请假
        uni.navigateTo({
          url: `/pages/task/qingjiaApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 1) {
        // 出差
        uni.navigateTo({
          url: `/pages/task/chuchaiApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 2) {
        // 加班
        uni.navigateTo({
          url: `/pages/task/jiabanApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 3) {
        // 入职
        uni.navigateTo({
          url: `/pages/task/ruzhiApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 4) {
        // 转正
        uni.navigateTo({
          url: `/pages/task/zhuanzhengApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 5) {
        // 离职
        uni.navigateTo({
          url: `/pages/task/lizhiApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 6) {
        // 督办单
        uni.navigateTo({
          url: `/pages/task/dubanApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 7) {
        // 会议室
        uni.navigateTo({
          url: `/pages/task/meetingroomApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 8) {
        // 接待单
        uni.navigateTo({
          url: `/pages/task/jiedaiApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 9) {
        // 加油
        uni.navigateTo({
          url: `/pages/task/jiayouApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 10) {
        // 用车
        uni.navigateTo({
          url: `/pages/task/yongcheApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 11) {
        // 用印
        uni.navigateTo({
          url: `/pages/task/yongyinApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 12) {
        // 督办任务 督办通过之后创建
        uni.navigateTo({
          url: `/pages/task/lizhiApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 13) {
        // 物资采购
        uni.navigateTo({
          url: `/pages/task/caigouApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 14) {
        // 物资领用
        uni.navigateTo({
          url: `/pages/task/wuzilingyongDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 15) {
        // 合同
        uni.navigateTo({
          url: `/pages/task/hetongShenpiDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 16) {
        // 招标
        uni.navigateTo({
          url: `/pages/task/zhaobiaoShenheDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 17) {
        // 传阅文件
        uni.navigateTo({
          url: `/pages/task/chuanyueWenjianDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 18) {
        // 公文流转
        uni.navigateTo({
          url: `/pages/task/gongwenLiuzhuanDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 19) {
        // 补卡申请
        uni.navigateTo({
          url: `/pages/task/bukaApplyDetail?id=${this.listYiwancheng[idx].relevenceId}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 24) {
        // 新建事项
        uni.navigateTo({
          url: `/pages/task/xinjianfawenDetail?id=${this.listYiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 25) {
        // 宣传报道
        uni.navigateTo({
          url: `/pages/task/xuanchuanbaodaoDetail?id=${this.listYiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      } else if (this.listYiwancheng[idx].typ == 26) {
        // 工作餐
        uni.navigateTo({
          url: `/pages/task/gongzuocanDetail?id=${this.listYiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listYiwancheng[idx].id}&see=1`
        });
      }
    },
    toAuditWeiwancheng(idx) {
      console.log("audit weiwancheng", idx, this.listWeiwancheng);
      if (this.listWeiwancheng[idx].typ == 0) {
        // 请假
        uni.navigateTo({
          url: `/pages/task/qingjiaApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 1) {
        // 出差
        uni.navigateTo({
          url: `/pages/task/chuchaiApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 2) {
        // 加班
        uni.navigateTo({
          url: `/pages/task/jiabanApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 3) {
        // 入职
        uni.navigateTo({
          url: `/pages/task/ruzhiApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 4) {
        // 转正
        uni.navigateTo({
          url: `/pages/task/zhuanzhengApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 5) {
        // 离职
        uni.navigateTo({
          url: `/pages/task/lizhiApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 6) {
        // 督办单
        uni.navigateTo({
          url: `/pages/task/dubanApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 7) {
        // 会议室
        uni.navigateTo({
          url: `/pages/task/meetingroomApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 8) {
        // 接待单
        uni.navigateTo({
          url: `/pages/task/jiedaiApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 9) {
        // 加油
        uni.navigateTo({
          url: `/pages/task/jiayouApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 10) {
        // 用车
        uni.navigateTo({
          url: `/pages/task/yongcheApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 11) {
        // 用印
        uni.navigateTo({
          url: `/pages/task/yongyinApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 12) {
        // 督办任务 督办通过之后创建
        uni.navigateTo({
          url: `/pages/task/lizhiApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 13) {
        // 物资采购
        uni.navigateTo({
          url: `/pages/task/caigouApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 14) {
        // 物资领用
        uni.navigateTo({
          url: `/pages/task/wuzilingyongDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 15) {
        // 合同
        uni.navigateTo({
          url: `/pages/task/hetongShenpiDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 16) {
        // 招标
        uni.navigateTo({
          url: `/pages/task/zhaobiaoShenheDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 17) {
        // 传阅文件
        uni.navigateTo({
          url: `/pages/task/chuanyueWenjianDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 18) {
        // 公文流转
        uni.navigateTo({
          url: `/pages/task/gongwenLiuzhuanDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 19) {
        // 补卡申请
        uni.navigateTo({
          url: `/pages/task/bukaApplyDetail?id=${this.listWeiwancheng[idx].relevenceId}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 24) {
        // 新建事项
        uni.navigateTo({
          url: `/pages/task/xinjianfawenDetail?id=${this.listWeiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 25) {
        // 宣传报道
        uni.navigateTo({
          url: `/pages/task/xuanchuanbaodaoDetail?id=${this.listWeiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      } else if (this.listWeiwancheng[idx].typ == 26) {
        // 工作餐
        uni.navigateTo({
          url: `/pages/task/gongzuocanDetail?id=${this.listWeiwancheng[idx].relevenceId}&items=${this.currentIndex}&taskid=${this.listWeiwancheng[idx].id}&see=1`
        });
      }
    },
    onClickItem(e) {
      console.log(e);
      if (this.currentIndex !== e.currentIndex) {
        this.currentIndex = e.currentIndex;
      }
    },
    // 获取最新的任务
    async getNewestTasksYiwancheng() {
      try {
        if (this.listYiwancheng.length == 0) {
          let res = await websock.getTaskListYiwancheng({ pageCount: 10 });
          console.log("getNewestTasks list empty", res);
          uni.stopPullDownRefresh();
          this.listYiwancheng = [...res, ...this.listYiwancheng];
        } else {
          let res = await websock.getTaskListYiwancheng({
            greaterThan: this.listYiwancheng[0].id,
            pageCount: 10
          });
          console.log("getNewestTasks list not empty", res);
          uni.stopPullDownRefresh();
          this.listYiwancheng = [...res, ...this.listYiwancheng];
        }
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 获取老的数据
    async getOlderTasksYiwancheng() {
      this.moreStatusYiwancheng = "loading";
      try {
        if (this.listYiwancheng.length == 0) {
          let res = await websock.getTaskListYiwancheng({ pageCount: 10 });
          console.log("getOlderTasks list empty", res);
          this.listYiwancheng = [...this.listYiwancheng, ...res];
          if (res.length < 10) {
            this.moreStatusYiwancheng = "noMore";
          } else {
            this.moreStatusYiwancheng = "more";
          }
        } else {
          let res = await websock.getTaskListYiwancheng({
            lessThan: this.listYiwancheng[this.listYiwancheng.length - 1].id,
            pageCount: 10
          });
          console.log("getOlderTasks list not empty", res);
          this.listYiwancheng = [...this.listYiwancheng, ...res];
          if (res.length < 10) {
            this.moreStatusYiwancheng = "noMore";
          } else {
            this.moreStatusYiwancheng = "more";
          }
        }
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 获取最新的任务
    async getNewestTasksWeiwancheng() {
      try {
        if (this.listWeiwancheng.length == 0) {
          let res = await websock.getTaskListWeiwancheng({ pageCount: 10 });
          console.log("getNewestTasks list empty", res);
          uni.stopPullDownRefresh();
          this.listWeiwancheng = [...res, ...this.listWeiwancheng];
        } else {
          let res = await websock.getTaskListWeiwancheng({
            greaterThan: this.listWeiwancheng[0].id,
            pageCount: 10
          });
          console.log("getNewestTasks list not empty", res);
          uni.stopPullDownRefresh();
          this.listWeiwancheng = [...res, ...this.listWeiwancheng];
        }
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 获取老的数据
    async getOlderTasksWeiwancheng() {
      this.moreStatusWeiwancheng = "loading";
      try {
        if (this.listWeiwancheng.length == 0) {
          let res = await websock.getTaskListWeiwancheng({ pageCount: 10 });
          console.log("getOlderTasks list empty", res);
          this.listWeiwancheng = [...this.listWeiwancheng, ...res];
          if (res.length < 10) {
            this.moreStatusWeiwancheng = "noMore";
          } else {
            this.moreStatusWeiwancheng = "more";
          }
        } else {
          let res = await websock.getTaskListWeiwancheng({
            lessThan: this.listWeiwancheng[this.listWeiwancheng.length - 1].id,
            pageCount: 10
          });
          console.log("getOlderTasks list not empty", res);
          this.listWeiwancheng = [...this.listWeiwancheng, ...res];
          if (res.length < 10) {
            this.moreStatusWeiwancheng = "noMore";
          } else {
            this.moreStatusWeiwancheng = "more";
          }
        }
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 新任务
    taskNew() {
      this.listWeiwancheng = [];
      this.listYiwancheng = [];
      this.getNewestTasksYiwancheng();
      this.getNewestTasksWeiwancheng();
    },
    // 获取格式化时间字符串
    getFormatedTimeStr(stamp, str = "MM-DD HH:mm:ss") {
      return moment(stamp).format(str);
    }
  }
};
</script>

<style>
@import url("../common/common.css");
</style>
